//
//  DXNewfeatureVC.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/9.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit
let count = 4

class DXNewfeatureVC: UIViewController,UIScrollViewDelegate {
    
  lazy var scrollView:UIScrollView = {
//    scrollView懒加载
    let scrollView = UIScrollView()
    scrollView.frame = self.view.frame
//    scrollView.backgroundColor = UIColor.redColor()
    scrollView.bounces = false
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.pagingEnabled = true
    return scrollView
    
    }()
//    懒加载pageControl
    lazy var pageControl:UIPageControl = {
    let  pageControl = UIPageControl()
     pageControl.numberOfPages = count
     pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
     pageControl.pageIndicatorTintColor = UIColor.darkGrayColor()
    return pageControl
    }()
//    懒加载enterBtn
    var enterBtn:UIButton = {
    let enterBtn = UIButton()
    enterBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
     enterBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
    enterBtn.setTitle("进入微博", forState: .Normal)
    enterBtn.sizeToFit()
 
    return enterBtn
    }()
    
    var shareBtn:UIButton = {
        let shareBtn = UIButton(type: UIButtonType.Custom)
        shareBtn.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        shareBtn.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        
        shareBtn.setTitle("分享到微博", forState: .Normal)
        shareBtn.sizeToFit()
        shareBtn.setTitleColor(UIColor.darkGrayColor(), forState:.Normal)
        shareBtn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: -5)
        return shareBtn
    }()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.delegate = self
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        setUpUI()
       
    }

    private func setUpUI(){
        let width =  view.frame.width
        let height =  view.frame.height
        pageControl.center =  CGPointMake(width*0.5, height - 100)
        for i in 0 ..< count {
        let imgView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            imgView.sizeToFit()
         
        imgView.frame = CGRectMake(CGFloat(i) * width, 0, width, height)
        scrollView.addSubview(imgView)
        scrollView.contentSize = CGSizeMake(width * CGFloat(count), height)
            
            if i == (count - 1){
            imgView.addSubview(enterBtn)
            imgView.userInteractionEnabled = true
            enterBtn.addTarget(self, action: "enterBtnClick", forControlEvents: .TouchUpInside)
    
            enterBtn.center = CGPointMake(width*0.5,  height - 150)
                
              imgView.addSubview(shareBtn)
            shareBtn.addTarget(self, action: "shareBtnClick:", forControlEvents: .TouchUpInside)
              shareBtn.center = CGPointMake(width*0.5,  height - 200)
                
            }
            
        }
        
    }
    
    // MARK: - 分享按钮 进入按钮的点击事件
    @objc private func shareBtnClick(sender:UIButton){
        sender.selected = !sender.selected
        
    }
    @objc private func enterBtnClick(){
        NSNotificationCenter.defaultCenter().postNotificationName(KChangeRootController, object: self)
        
    }
    
}

extension DXNewfeatureVC{

    func scrollViewDidScroll(scrollView: UIScrollView) {
        let width:CGFloat =  UIScreen.mainScreen().bounds.width
       let page = Int(scrollView.contentOffset.x / width + 0.5)
        
        pageControl.currentPage = page
        
        
    }


}
















